package co.yixiang.modules.system.rest;

import co.yixiang.annotation.AnonymousAccess;
import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.BusinessCooperation;
import co.yixiang.modules.system.service.BusinessCooperationService;
import co.yixiang.modules.system.dto.queryCriteria.BusinessCooperationQueryCriteria;
import co.yixiang.modules.system.dto.BusinessCooperationDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author db
 * @date 2020-11-19
 */
@AllArgsConstructor
@Api(tags = "商户合作管理")
@RestController
@RequestMapping("/api/businessCooperation")
public class BusinessCooperationController {

    private final BusinessCooperationService businessCooperationService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','businessCooperation:list')")
    public void download(HttpServletResponse response, BusinessCooperationQueryCriteria criteria) throws IOException {
        businessCooperationService.download(Convert.toList(BusinessCooperationDto.class, businessCooperationService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询商户合作")
    @ApiOperation("查询商户合作")
    @PreAuthorize("@el.check('admin','businessCooperation:list')")
    public ResponseEntity<Object> getBusinessCooperations(BusinessCooperationQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(businessCooperationService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增商户合作")
    @ApiOperation("新增商户合作")
    @PreAuthorize("@el.check('admin','businessCooperation:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody BusinessCooperation resources) {
        return new ResponseEntity<>(businessCooperationService.save(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改商户合作")
    @ApiOperation("修改商户合作")
    @PreAuthorize("@el.check('admin','businessCooperation:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody BusinessCooperation resources) {
        businessCooperationService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除商户合作")
    @ApiOperation("删除商户合作")
    @PreAuthorize("@el.check('admin','businessCooperation:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        businessCooperationService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("新增商户合作")
    @ApiOperation("新增商户合作")
    @PostMapping("/join")
    @AnonymousAccess
    public ResponseEntity<Object> join(@Validated @RequestBody BusinessCooperationDto resources) {
        return ResponseEntity.ok(businessCooperationService.join(resources));
    }
}